<?php

ignore_user_abort(true); // 后台运行，不受前端断开连接影响
set_time_limit(0);
ini_set('max_execution_time', '0');
chdir(dirname(__FILE__));
error_reporting(E_ERROR);
require_once ("../log.php");
require_once ("../config.php");//引入配置ss
require_once ("../api/api_tool.php");

/** 
 * @name Api接口demo（php版）
 * @desc 本接口列表，只是用于系统处理机器人行为
 * 一分钟执行一次，给符合条件的机器人发送
 */

// "2020-06-28 12:07:00"

// 当前时间
$now_time = simQuery("SELECT NOW() AS nowdate")['nowdate'];

// 截取出当前的时和分
$hm_time = substr($now_time, 11, 5);
$before_time = substr($now_time, 0, 11)."12:00:00";

if ($_GET['sign'] == '1') {
    // 刷新所有群的sign标志
    simQuery("UPDATE wechat_group SET signed = '0';");
}

$send_bot_list = array();
$wx_group = AllQuery("SELECT rot.id AS bot_id, wx.id AS wx_id, rot.server_id AS server_id, ser.host AS host, ser.port AS port FROM wechat_group AS wx, robot AS rot, server AS ser WHERE wx.robot_wxid = rot.wechat_id AND rot.service_period > '{$now_time}' AND wx.status = 1 AND rot.server_id = ser.id AND wx.signed = '0';");
if (!empty($wx_group)) {
    for ($wx_num = 0; $wx_num < count($wx_group); ++$wx_num) {
        if (empty($send_bot_list[$wx_group['bot_id']])) {
            $send_bot_list[$wx_group['bot_id']] = true;
            $wx_data = $wx_group[$wx_num];
            $host = $wx_data['host'];
            $port = $wx_data['port'];
            $url  = "/api/api_accept_post.php";
            $data = array();
            $data['id']             = $wx_data['wx_id'];
            $data['text']           = "===下午爆款===";
            $data['end_text']       = "下午没有时间看群的话，现在可以去爬楼梯了";
            $data['now_time']       = $now_time;
            $data['before_time']    = $before_time;
            $data['fun']            = "send_ladder_data";
            $data['sign']           = generateSign($data);
            sendFZSHttp($host, $port, $url, $data);
            // 设置为已经发送
            simQuery("UPDATE wechat_group SET signed = '1' WHERE id = {$wx_data['wx_id']};");
        }
    }
}

exit();

?>